//
//  CompleteViewController2.m
//  inquiry
//
//  Created by 高宇 on 2020/6/22.
//  Copyright © 2020 高宇. All rights reserved.
//

#define SCREEN_WIDTH                 ([[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT                ([[UIScreen mainScreen] bounds].size.height)
#define WEAKSELF   __typeof(&*self) __weak weakSelf = self;

#import "CompleteViewController2.h"
#import "PersonalView.h"
#import "SheelViewController.h"
#import "GXToast.h"
#import "ULNetHelper.h"
#import "Interface.h"
#import "NSString+MD5.h"
#import "JsonObject.h"

@interface CompleteViewController2 ()<PersonalDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) PersonalView *moneyView;
@property (nonatomic, strong) PersonalView *fatherView;
@property (nonatomic, strong) PersonalView *momView;
@property (nonatomic, strong) PersonalView *scoreView;
@property (nonatomic, strong) UILabel *gloryLabel;
@property (nonatomic, strong) UIView *gloryView;
@property (nonatomic, strong) UITextView *gloryTextView;
@property (nonatomic, strong) UIButton *submitButton;
@end

@implementation CompleteViewController2

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    //创建UI
    [self createUI];
}

- (void)createUI{
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, CGRectGetMaxY(_submitButton.frame));
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.scrollView];
    
    int gao = 40;
    if (@available(iOS 11.0, *)) {
        UIWindow *mainWindow = [[UIApplication sharedApplication] keyWindow];
        if (mainWindow.safeAreaInsets.bottom > 0.0) {
            gao = 64;
        }
    }
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(30, gao, SCREEN_WIDTH-60, 33)];
    label.text = @"请完善个人资料";
    label.font = [UIFont systemFontOfSize:24];
    label.textColor = [UIColor blackColor];
    [self.scrollView addSubview:label];
    
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(33, gao+42, SCREEN_WIDTH-60, 22)];
    label2.text = @"温馨提示，请在家长的帮助下完成～";
    label2.font = [UIFont systemFontOfSize:16];
    label2.textColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1];
    [self.scrollView addSubview:label2];
    
    //第二页
    self.moneyView = [[PersonalView alloc] initWithFrame:CGRectMake(30, gao+90, SCREEN_WIDTH-60, 48)];
    self.moneyView.label.text = @"家庭收入";
    self.moneyView.delegate = self;
    [self.scrollView addSubview:self.moneyView];
    
    self.fatherView = [[PersonalView alloc] initWithFrame:CGRectMake(30, CGRectGetMaxY(self.moneyView.frame)+20, SCREEN_WIDTH-60, 48)];
    self.fatherView.label.text = @"父亲学历";
    self.fatherView.delegate = self;
    [self.scrollView addSubview:self.fatherView];
    
    self.momView = [[PersonalView alloc] initWithFrame:CGRectMake(30, CGRectGetMaxY(self.fatherView.frame)+20, SCREEN_WIDTH-60, 48)];
    self.momView.label.text = @"母亲学历";
    self.momView.delegate = self;
    [self.scrollView addSubview:self.momView];
    
    self.scoreView = [[PersonalView alloc] initWithFrame:CGRectMake(30, CGRectGetMaxY(self.momView.frame)+20, SCREEN_WIDTH-60, 48)];
    self.scoreView.label.text = @"学生成绩";
    self.scoreView.delegate = self;
    [self.scrollView addSubview:self.scoreView];
    
    self.gloryLabel = [[UILabel alloc] initWithFrame:CGRectMake(33, CGRectGetMaxY(self.scoreView.frame)+40, SCREEN_WIDTH-60, 70)];
    self.gloryLabel.font = [UIFont systemFontOfSize:16];
    self.gloryLabel.textColor = [UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1.0];
    self.gloryLabel.numberOfLines = 0;
    self.gloryLabel.text = @"是否具有等级，技能或各类荣誉证书（钢琴乐器等级，书法绘画，志愿者，优秀学生等）";
    [self.scrollView addSubview:self.gloryLabel];
    
    self.gloryView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.gloryLabel.frame)+20, SCREEN_WIDTH, 150)];
    self.gloryView.backgroundColor = [UIColor colorWithRed:248/255.0 green:248/255.0 blue:248/255.0 alpha:1.0];
    [self.scrollView addSubview:self.gloryView];
    
    self.gloryTextView = [[UITextView alloc] initWithFrame:CGRectMake(33, 25, SCREEN_WIDTH-60, self.gloryView.frame.size.height-50)];
    self.gloryTextView.font = [UIFont systemFontOfSize:16];
    self.gloryTextView.backgroundColor = [UIColor clearColor];
    self.gloryTextView.textColor = [UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1.0];
    self.gloryTextView.text = @"";
    [self.gloryView addSubview:self.gloryTextView];
    
    self.submitButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.submitButton.frame = CGRectMake(self.view.frame.size.width/2-75, CGRectGetMaxY(self.gloryView.frame)+40, 150, 40);
    [self.submitButton setTitle:@"提交" forState:0];
    [self.submitButton setTitleColor:[UIColor whiteColor] forState:0];
    self.submitButton.backgroundColor = [UIColor colorWithRed:110/255.0 green:132/255.0 blue:238/255.0 alpha:1];
    self.submitButton.layer.cornerRadius = 10;
    [self.submitButton addTarget:self action:@selector(submitClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:self.submitButton];
    
    
    //最后设置contentSize
    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, CGRectGetMaxY(_submitButton.frame)+40);
}

- (void)submitClick:(UIButton *)button{
    button.enabled = NO;
    
    NSString *gradeId = self.gradeStr;
    NSString *classesId = self.classStr;
    NSString *schoolId = self.schoolStr;
    NSString *studentName = self.nameStr;
    NSString *uuid = self.identityId;
    NSString *gender = self.sexStr;  //性别
    
    NSDictionary *userInfo = @{
        @"gender":[JsonObject fuckNULL:gender],
        @"age":[JsonObject fuckNULL:self.numStr],
        @"household_income":[JsonObject fuckNULL:self.moneyView.idStr],
        @"father_edu":[JsonObject fuckNULL:self.fatherView.idStr],
        @"mother_edu":[JsonObject fuckNULL:self.momView.idStr],
        @"academic_performance":[JsonObject fuckNULL:self.scoreView.idStr],
        @"other":[JsonObject fuckNULL:self.gloryTextView.text],
    };
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:userInfo options:NSJSONWritingPrettyPrinted error:nil];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"gradeId"] = gradeId;
    dic[@"classesId"] = classesId;
    dic[@"schoolId"] = schoolId;
    dic[@"studentId"] = uuid;
    dic[@"studentName"] = studentName;
    dic[@"userInfo"] = jsonString;
    
    [ULNetHelper POST:updateStudentInfoPath parameters:dic success:^(id responseObject) {
        button.enabled = YES;
        NSDictionary *dic = responseObject;
        long code = [dic[@"code"] longValue];
        if (code == 200) {
            [GXToast showText:@"个人资料填写完毕"];
            NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
            [user setObject:studentName forKey:@"studentName"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                UIViewController *parentVC = self.presentingViewController;
                UIViewController *bottomVC;
                while (parentVC) {
                    bottomVC = parentVC;
                    parentVC = parentVC.presentingViewController;
                }
                [bottomVC dismissViewControllerAnimated:YES completion:nil];
            });
        }else{
            NSString *msg = responseObject[@"msg"];
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"资料填写失败" message: msg preferredStyle:UIAlertControllerStyleAlert];
            [alert addAction:[UIAlertAction actionWithTitle:@"关闭" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
                
            }]];
            [self presentViewController:alert animated:YES completion:nil];
            
        }
    }];
    
    
}

#pragma mark personal代理
- (void)personalClick:(UIView *)personalView{
    NSArray *arr;
    if (personalView == _moneyView) {
        arr = @[@{@"name":@"0-8万",@"id":@"0-8"},
        @{@"name":@"9-15万",@"id":@"9-15"},
        @{@"name":@"16-32万",@"id":@"16-32"},
        @{@"name":@"32万以上",@"id":@"32+"}];
    }else if(personalView == _fatherView) {
        arr = @[@{@"name":@"初中及以下",@"id":@"junior high"},
        @{@"name":@"高中（中专）",@"id":@"senior middle"},
        @{@"name":@"大专",@"id":@"college"},
        @{@"name":@"大学本科",@"id":@"university undergraduate"},
        @{@"name":@"硕士研究生",@"id":@"master's degree"},
        @{@"name":@"博士及以上",@"id":@"doctor"},];
    }else if(personalView == _momView) {
        arr = @[@{@"name":@"初中及以下",@"id":@"junior high"},
        @{@"name":@"高中（中专）",@"id":@"senior middle"},
        @{@"name":@"大专",@"id":@"college"},
        @{@"name":@"大学本科",@"id":@"university undergraduate"},
        @{@"name":@"硕士研究生",@"id":@"master's degree"},
        @{@"name":@"博士及以上",@"id":@"doctor"},];
    }else if(personalView == _scoreView) {
        arr = @[@{@"name":@"年级前10%",@"id":@"10%"},
                @{@"name":@"年级前11%-30%",@"id":@"11%-30%"},
                @{@"name":@"年级前31%-50%",@"id":@"31%-50%"},
                @{@"name":@"年级前51%-70%",@"id":@"51%-70%"},
                @{@"name":@"年级前71%-90%",@"id":@"71%-90"},
                @{@"name":@"年级前91%-100%",@"id":@"91%-100%"},];
    }
    
    SheelViewController *vc = [[SheelViewController alloc] init];
    vc.arr = arr;
    [[UIApplication sharedApplication].keyWindow addSubview:vc.view];
    [self addChildViewController:vc];
    
    __typeof(&*vc) __weak weakVC = vc;
    vc.block = ^{
        [weakVC.view removeFromSuperview];
        [weakVC removeFromParentViewController];
    };
    
    PersonalView *perView = (PersonalView *)personalView;
    vc.tureblock = ^(NSDictionary *dic) {
        perView.mainlabel.text = dic[@"name"];
        perView.idStr = dic[@"id"];
        [weakVC.view removeFromSuperview];
        [weakVC removeFromParentViewController];
    };
}

- (NSString *)nowTimeSwitchTimestamp{
    NSDate *datenow = [NSDate date];//现在时间
    //这里如果long不够用,就用(long long)
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)([datenow timeIntervalSince1970]*1000)];
    return timeSp;
}


@end
